//
//  CheckTool.m
//  项目的登录注册模块的封装
//
//  Created by 孙鹏飞 on 2017/3/20.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "CheckTool.h"

@implementation CheckTool


/**
 *判断手机号的格式
 */
+ (BOOL)isPhoneNumber:(NSString *)phoneNumber
{
    NSString* number=@"^1[3|4|5|7|8][0-9]\\d{8}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:phoneNumber];
}

/** 检查手机号,输出提示 */
+ (BOOL)checkMobile:(NSString *)phoneNumber
{
    if (phoneNumber.length == 0) {
        [HMHUDManager showHUDWithText:@"手机号不能为空"];
        return NO;
    }else if (![CheckTool isPhoneNumber:phoneNumber]) {
        // 显示提示
        [HMHUDManager showHUDWithText:@"请输入正确的手机号"];
        return NO;
    }
    return YES;
}

/**
 *判断密码的格式
 */
+ (BOOL)isPassword:(NSString *)password
{
    NSString *passwordCode = @"^(?![0-9]+$)(?![a-zA-Z]+$)[\\x20-\\x7e]{6,16}$";
    NSPredicate *phonetTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordCode];
    return [phonetTest evaluateWithObject:password];
}

/** 检查密码 */
+ (BOOL)checkPassword:(NSString *)password
{
    if (password.length == 0) {
        [HMHUDManager showHUDWithText:@"密码不能为空"];
        return NO;
    }else if (password.length < 6){
        [HMHUDManager showHUDWithText:@"密码不能少于6位"];
        return NO;
    }else if (password.length > 16){
        [HMHUDManager showHUDWithText:@"密码不能大于16位"];
        return NO;
    }else if(![CheckTool isPassword:password]){
            [HMHUDManager showHUDWithText:@"字母、数字和标点符号至少包含2种"];
            return NO;
    }
    return YES;
}

/**
 生成6位随机数的验证码
 */
+ (NSString *)getVerificationCode
{
    NSUInteger number = 0;
    NSString * string = @"";
    NSString * testString;
    for (int i = 0; i < 6; i++) {
        number = arc4random() % 9;
        NSLog(@"...%ld",number);
        string = [NSString stringWithFormat:@"%@%ld",string,number];
    }
    
    testString = string;
    return testString;
}



@end
